<?php

namespace App\Http\Controllers\Admin;

use App\Models\Problems;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HelpController extends Controller
{


    /**
     * 后台用户
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  index()
    {



        return view('admin/problem/index');

    }

    /**
     * 编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  edit(Request $request)
    {
        $params = $request->only(['problems_id']);
        $validator = \Validator::make($params, [
            'problems_id' => 'required|exists:problems,id',

        ], [
            'problems_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data =  Problems::find($request->problems_id);

        return view('admin/problem/edit',['data'=>$data]);
    }

    /**
     * 添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  add()
    {


        return view('admin/problem/add');
    }





    /**
     * 帮助中心
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $searchParams = json_decode($request->searchParams, true);
        $data = Problems::Title($searchParams['title']??'')->orderBy('id', 'desc')->paginate($request->limit)->toArray();

        return $this->success(['data'=>$data['data'], 'count'=> $data['total'],'code'=>0]);
    }

    /**
     * 帮助详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $params = $request->only(['problems_id']);
        $validator = \Validator::make($params, [
            'problems_id' => 'required|exists:problems,id',
        ], [
            'problems_id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $problem = Problems::find($request->problems_id);

        return $this->success($problem);
    }


    /**
     * 新增常见问题
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addHelp(Request $request)
    {
        $params = $request->only(['problem', 'answer']);
        $validator = \Validator::make($params, [
            'problem' => 'required|string|max:100',
            'answer' => 'required',
        ], [
            'problem.*' => '标题参数错误',
            'answer.*' => '内容参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = [
            'problem' => $request->problem,
            'answer' => $request->answer,
        ];
        $re = Problems::add($data);
        if ($re) {
            return $this->success(['msg' => '添加成功']);
        }
        return $this->error('创建失败');
    }


    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveHelp(Request $request)
    {
        $params = $request->only(['problem', 'answer', 'problems_id']);
        $validator = \Validator::make($params, [
            'problems_id' => 'required|exists:problems,id',
            'problem' => 'required|string|max:100',
            'answer' => 'required',
        ], [
            'problems_id.*' => '参数错误',
            'problem.*' => '参数错误',
            'answer.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = [
            'problem' => $request->problem,
            'answer' => $request->answer,
        ];
        $id = $request->problems_id;
        $re = Problems::edit($id, $data);
        if ($re) {
            return $this->success(['msg' => '成功']);
        }
        return $this->error('修改失败');

    }

    /**
     * 删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        $params = $request->only(['problems_id']);
        $validator = \Validator::make($params, [
            'problems_id' => 'required|exists:problems,id',
        ], [
            'problems_id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $problems = Problems::find($request->problems_id);

        $problems->delete();
        if ($problems->trashed()) {
            return $this->success(['msg' => '保存成功']);
        }
        return $this->error('保存失败');
    }
}
